<template>
  <el-dropdown trigger="click" @command="handleLanguageChange">
    <div class="lang-select--style">
      <svg-icon icon-class="language" />
    </div>
    <template #dropdown>
      <el-dropdown-menu>
        <el-dropdown-item :key="locale.lang" v-for="locale in appStore.messages" :disabled="appStore.language === locale.lang" :command="locale.lang">
          {{locale.name}}
        </el-dropdown-item>
      </el-dropdown-menu>
    </template>
  </el-dropdown>
</template>

<script setup lang="ts">
import { useI18n } from 'vue-i18n'
import SvgIcon from '@/components/SvgIcon/index.vue'
import { useAppStore } from '@/store/modules/app'

const appStore = useAppStore()

const {  t, locale } = useI18n()

const handleLanguageChange = (lang: string) => {
  locale.value = lang
  appStore.changeLanguage(lang)
  ElMessage.success(t('language.switchLanguageSuccessful'))
  location.reload()
}
</script>

<style lang="scss" scoped>
.lang-select--style {
  font-size: 18px;
  line-height: 50px;
}
</style>
